//
//  ClassRoomListViewController.m
//  AndCampus
//
//  Created by 史宵宵 on 2018/1/15.
//  Copyright © 2018年 史宵宵. All rights reserved.
//

#import "ClassRoomListViewController.h"
#import "ClassRoomTableViewCell.h"
#import "ClassRoomDetailViewController.h"

@interface ClassRoomListViewController ()
@property (nonatomic,strong) NSMutableArray *dataArray;
@end

@implementation ClassRoomListViewController
-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    self.tableView.frame = CGRectMake(0, 0, kScreenW, kScreenH - 64 - 50 - 41);
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([ClassRoomTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"ClassRoomCell"];
     [self setupRefresh];//刷新
}
#pragma mark - 获取课程列表
- (void)creatDataList{
    YJNetWorkManger *yjNetWorkManger =  [YJNetWorkManger shareManager];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setValue:[NSString stringWithFormat:@"%ld",(long)self.pageNo] forKey:@"page"]; //分页
    [dic setValue:KREFRESHROWS forKey:@"rows"];   //每页数量
    [dic setValue:[Global getUserTokenInfo] forKey:@"token"]; //用户的token
    [dic setValue:self.subjectIDStr forKey:@"subjectId"];    //科目的id
    [yjNetWorkManger requestType:POST andURL:[JSPathUtil urlAppCourseGetCourseBySubject] andParmas:dic andComplition:^(id response, BOOL isuccess) {
        if (isuccess) {
            if (self.pageNo == 1)
            {
                [self.dataArray removeAllObjects];
            }
            NSArray *array = [NSArray arrayWithArray:response[@"list"]];
            
            [self.dataArray addObjectsFromArray:[ClassRoomInfo mj_objectArrayWithKeyValuesArray:array]];
            if (self.dataArray.count <= 0 ) {
                self.noDataImageView.image = [UIImage imageNamed:@"noData.png"];
                self.noDataImageView.hidden = NO;
                self.noDataLabel.text = @"还没有课程";
                self.noDataLabel.hidden = NO;
            }else{
                self.noDataImageView.hidden = YES;
                self.noDataLabel.hidden = YES;
            }
            if (self.pageNo < [response[@"totalRow"] intValue])
            {
                self.isMorePage = YES;
            }
            else
            {
                self.isMorePage = NO;
            }
            
            if (self.type == 1)
            {
                [self.tableView.mj_header endRefreshing];
            }
            else if (self.type == 2)
            {
                [self.tableView.mj_footer endRefreshing];
            }
            [self.tableView reloadData];
        }
    }];
}
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    ClassRoomTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"ClassRoomCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.classRoomInfo = self.dataArray[indexPath.section];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 110;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (self.tableViewScrollBlack) {
        self.tableViewScrollBlack(self.tableView.contentOffset);
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
    ClassRoomDetailViewController *classRoomDetailVC = [[ClassRoomDetailViewController alloc]init];
    ClassRoomInfo *classRoomInfo = self.dataArray[indexPath.section];
    classRoomDetailVC.courseId = classRoomInfo.course_id;
    [self.navigationController pushViewController:classRoomDetailVC animated:YES];
    
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    view.frame = CGRectMake(0, 0, kScreenW, 0.01);
//    view.backgroundColor = [UIColor whiteColor];
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01f;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
